﻿
using iTextSharp.text.pdf;
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Xml.Linq;

public class ht_bc
{
    public static void InputExcel(Page b_f, DataSet b_ds, string b_url, string b_tenxml)
    {
        string filename = HttpContext.Current.Server.MapPath(b_url + "/" + b_tenxml + ".xml");
        string path = "~/Outputs/" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + "_" + b_tenxml + ".xls";
        string str3 = HttpContext.Current.Server.MapPath(path);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExcelMLFiller filler = new CExcelMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform();
            if (filler.OperationFailed && (filler.ErrorList.Count > 0))
            {
                return;
            }
        }
        else if (filler.ErrorList.Count > 0)
        {
            return;
        }
        filler.ExcelMLDocument.Save(str3);
        b_f.Response.Redirect(path, false);
    }

    public static string[] P_DOC_BM(string b_mau)
    {
        XmlDocument document = new XmlDocument();
        document.Load(HttpContext.Current.Server.MapPath(b_mau));
        string outerXml = document.OuterXml;
        string str2 = "";
        P_DOC_INDEX(ref str2, outerXml);
        if (str2 == "")
        {
            return new string[0];
        }
        return str2.Split(new char[] { ',' });
    }

    private static void P_DOC_INDEX(ref string b_bien, string b_mau)
    {
        int index = b_mau.IndexOf("w:type=\"Word.Bookmark.Start\" w:name=\"");
        if (index > 0)
        {
            index += "w:type=\"Word.Bookmark.Start\" w:name=\"".Length;
            string str = b_mau.Substring(index, b_mau.Length - index);
            int length = str.IndexOf(" />") - 2;
            string str2 = str.Substring(0, length);
            if (b_bien == "")
            {
                b_bien = str2;
            }
            else
            {
                b_bien = b_bien + "," + str2;
            }
            if (str.Length > 0)
            {
                P_DOC_INDEX(ref b_bien, str);
            }
        }
    }

    public static void P_MO(Page b_f, string b_url, string b_ten_bc, int b_width, int b_height, DataSet[] b_ds)
    {
        b_url = b_f.ResolveUrl(b_url);
        HttpContext.Current.Session["kq_ds"] = b_ds;
        ScriptManager.RegisterStartupScript(b_f, b_f.GetType(), "HTBC" + new Random().Next().ToString(), "window.open('" + b_url + ("',true, 'resizable=yes,scrollbars=yes,height=" + b_height.ToString() + "px,width=" + b_width.ToString() + "px')"), true);
    }

    public static void P_MO(Page b_f, string b_url, string b_ten_bc, int b_width, int b_height, DataSet b_ds)
    {
        b_url = b_f.ResolveUrl(b_url);
        HttpContext.Current.Session["kq_ds"] = b_ds;
        ScriptManager.RegisterStartupScript(b_f, b_f.GetType(), "HTBC" + new Random().Next().ToString(), "window.open('" + b_url + ("',true, 'resizable=yes,scrollbars=yes,height=" + b_height.ToString() + "px,width=" + b_width.ToString() + "px')"), true);
    }

    public static void P_MO(Page b_f, string b_url, string b_mau, string b_out, string b_loai_bc, string b_ten_bc, DataSet b_ds)
    {
        b_url = b_f.ResolveUrl(b_url) + "?loai=" + b_loai_bc + "&ten=" + b_ten_bc + "&mau=" + b_mau + "&out=" + b_out;
        HttpContext.Current.Session["kq_ds"] = b_ds;
        //form.P_MO(b_f, b_url, "HTBC" + new Random().Next().ToString());
    }

    public static void P_NEN_PDF(string b_file_in, string b_file_out)
    {
        PdfReader reader = new PdfReader(b_file_in);
        PdfStamper stamper = new PdfStamper(reader, new FileStream(b_file_out, FileMode.Create), '5') {
            FormFlattening = true
        };
        stamper.SetFullCompression();
        stamper.Close();
    }

    public static void P_XUAT_EXCEL(string b_mau, ref string b_out, string b_ten, DataSet b_ds)
    {
        string filename = HttpContext.Current.Server.MapPath(b_mau + b_ten + ".xml");
        b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".xls";
        string str2 = HttpContext.Current.Server.MapPath(b_out);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExcelMLFiller filler = new CExcelMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform();
            if (!filler.OperationFailed)
            {
                goto Label_00FC;
            }
            IEnumerator enumerator = filler.ErrorList.GetEnumerator();
            {
                while (enumerator.MoveNext())
                {
                    string current = (string) enumerator.Current;
                    return;
                }
                goto Label_00FC;
            }
        }
        IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
        {
            while (enumerator2.MoveNext())
            {
                string text2 = (string) enumerator2.Current;
                return;
            }
        }
    Label_00FC:
        filler.ExcelMLDocument.Save(str2);
    }

    public static void P_XUAT_EXCEL_ROW(string b_mau, ref string b_out, string b_ten, DataSet b_ds)
    {
        string filename = HttpContext.Current.Server.MapPath(b_mau + b_ten + ".xml");
        b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".xls";
        string str2 = HttpContext.Current.Server.MapPath(b_out);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExcelMLFiller filler = new CExcelMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform_dong();
            if (!filler.OperationFailed)
            {
                goto Label_00FC;
            }
            IEnumerator enumerator = filler.ErrorList.GetEnumerator();
            {
                while (enumerator.MoveNext())
                {
                    string current = (string) enumerator.Current;
                    return;
                }
                goto Label_00FC;
            }
        }
        IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
        {
            while (enumerator2.MoveNext())
            {
                string text2 = (string) enumerator2.Current;
                return;
            }
        }
    Label_00FC:
        filler.ExcelMLDocument.Save(str2);
    }


    public static void P_XUAT_WORD(string b_ddan, ref string b_out, string b_ten, DataSet b_ds)
    {
        string filename = HttpContext.Current.Server.MapPath(b_ddan + b_ten + ".xml");
        b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".doc";
        string str2 = HttpContext.Current.Server.MapPath(b_out);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExWordMLFiller filler = new CExWordMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform();
            if (!filler.OperationFailed)
            {
                goto Label_00FC;
            }
            IEnumerator enumerator = filler.ErrorList.GetEnumerator();
            {
                while (enumerator.MoveNext())
                {
                    string current = (string) enumerator.Current;
                    return;
                }
                goto Label_00FC;
            }
        }
        IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
        {
            while (enumerator2.MoveNext())
            {
                string text2 = (string) enumerator2.Current;
                return;
            }
        }
    Label_00FC:
        filler.WordMLDocument.Save(str2);
    }

    public static void P_XUAT_WORD_MAU(string b_mau, ref string b_out, string b_ten, DataTable b_dt)
    {
        try
        {
            b_mau = b_mau + b_ten + ".xml";
            b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".doc";
            XmlDocument document = new XmlDocument();
            document.Load(HttpContext.Current.Server.MapPath(b_mau));
            string outerXml = document.OuterXml;
            int length = outerXml.IndexOf("<wx:sect>") + 9;
            string str2 = outerXml.Substring(0, length);
            int index = outerXml.IndexOf("<w:sectPr>");
            string str3 = outerXml.Substring(index, outerXml.Length - index);
            string str4 = "";
            string str5 = outerXml;
            DataTable table = new DataTable();
            for (int i = 0; i < b_dt.Columns.Count; i++)
            {
                table.Columns.Add(b_dt.Columns[i].ColumnName.ToLower());
            }
            table.AcceptChanges();
            table.TableName = b_dt.TableName;
            for (int j = 0; j < b_dt.Rows.Count; j++)
            {
                string str6;
                outerXml = str5;
                DataSet dsData = new DataSet();
                table.Clear();
                table.Rows.Add(b_dt.Rows[j].ItemArray);
                dsData.Tables.Add(table.Copy());
                CExWordMLFiller filler = new CExWordMLFiller(dsData, outerXml);
                if (!filler.OperationFailed)
                {
                    filler.Transform();
                    if (!filler.OperationFailed)
                    {
                        goto Label_01E3;
                    }
                    IEnumerator enumerator = filler.ErrorList.GetEnumerator();
                    {
                        while (enumerator.MoveNext())
                        {
                            string current = (string) enumerator.Current;
                            return;
                        }
                        goto Label_01E3;
                    }
                }
                IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
                {
                    while (enumerator2.MoveNext())
                    {
                        string text2 = (string) enumerator2.Current;
                        return;
                    }
                }
            Label_01E3:
                str6 = filler.WordMLDocument.OuterXml;
                int num5 = str6.IndexOf("<w:sectPr>");
                str4 = str4 + str6.Substring(length, num5 - length) + "<w:p><w:r><w:rPr><w:sz w:val=\"28\" /><w:sz-cs w:val=\"28\" /></w:rPr><w:br w:type=\"page\" /></w:r></w:p>";
            }
            XDocument.Parse(str2 + str4 + str3).Save(HttpContext.Current.Server.MapPath(b_out));
        }
        catch (Exception exception)
        {
            throw new Exception(exception.ToString());
        }
    }

    public static void P_XUAT_WORD_ROW(string b_ddan, ref string b_out, string b_ten, DataSet b_ds)
    {
        string filename = HttpContext.Current.Server.MapPath(b_ddan + b_ten + ".xml");
        b_out = b_out + b_ten + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".doc";
        string str2 = HttpContext.Current.Server.MapPath(b_out);
        XmlDocument document = new XmlDocument();
        document.Load(filename);
        CExWordMLFiller filler = new CExWordMLFiller(b_ds, document.OuterXml);
        if (!filler.OperationFailed)
        {
            filler.Transform_row();
            if (!filler.OperationFailed)
            {
                goto Label_00FC;
            }
            IEnumerator enumerator = filler.ErrorList.GetEnumerator();
            {
                while (enumerator.MoveNext())
                {
                    string current = (string) enumerator.Current;
                    return;
                }
                goto Label_00FC;
            }
        }
        IEnumerator enumerator2 = filler.ErrorList.GetEnumerator();
        {
            while (enumerator2.MoveNext())
            {
                string text2 = (string) enumerator2.Current;
                return;
            }
        }
    Label_00FC:
        filler.WordMLDocument.Save(str2);
    }
}

